package com.heima.behavior.service.impl;

import com.heima.behavior.service.BehaviorService;
import com.heima.common.cache.CacheService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.behavior.ArticleReadCountDto;
import com.heima.model.behavior.BehaviorDto;
import com.heima.model.behavior.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.common.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

@Service
@Slf4j
public class BehaviorServiceImpl implements BehaviorService {

    @Autowired
    private CacheService cacheService;

    @Override
    public ResponseResult isLike(UnLikesBehaviorDto dto) {
        //校验参数
        if (ObjectUtils.isEmpty(dto)||dto.getType()<0||dto.getType()>1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //必须是登录状态下才能喜欢
        //获取userId 看是否为空
        Integer userId = AppThreadLocalUtil.getUser().getId();

        //若userId为空就不能喜欢
        if (StringUtils.isEmpty(userId)){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //将数据存入redis里 设置key value
        if (dto.getType()==0){
            //不喜欢
            //
            cacheService.hPut(BehaviorConstants.UN_LIKE_BEHAVIOR+dto.getArticleId()+"-"+userId + "",dto.getArticleId()+"",dto.toString());
        }
        else {
            //取消不喜欢
            cacheService.hDelete(BehaviorConstants.UN_LIKE_BEHAVIOR+dto.getArticleId()+"-"+userId + "",dto.getArticleId()+"");

        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    public ResponseResult read(ArticleReadCountDto dto) {
        //校验参数
        if (ObjectUtils.isEmpty(dto)||dto.getCount()<1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //必须是登录状态下才能评论
        //获取userId 看是否为空
        Integer userId = AppThreadLocalUtil.getUser().getId();
        //若userId为空就不能评论
        if (StringUtils.isEmpty(userId)){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        int count = dto.getCount();
        // 使用Redis的计数器功能来记录阅读次数点进一次+1
        String key = BehaviorConstants.READ_BEHAVIOR +dto.getArticleId()+"-"+userId + "";
        cacheService.incrBy(key,1L);

        // 如果count为1，表示这是第一次阅读，记录时间戳
     /*   double time = System.currentTimeMillis();
        if (count == 1) {
            cacheService.zAdd(key, dto.getArticleId()+"",time);
        }*/
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 点赞
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likeBehavior(BehaviorDto dto) {
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //从线程中拿到当前登录用户
        ApUser user = AppThreadLocalUtil.getUser();
        //判断是否登录
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        String key = BehaviorConstants.LIKE_BEHAVIOR + dto.getArticleId() + "/" + user.getId();
        //3.点赞  保存数据
        if (dto.getOperation() == 0) {
            cacheService.hPut(key, user.getId() + "", dto.toString());
        }

        if (dto.getOperation() == 1) {
            cacheService.hDelete(key, user.getId() + "");
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
